#if !defined(__HA_MQTT_H__)
#define __HA_MQTT_H__

#include <stdint.h>
#include <stdlib.h>

#pragma pack(1)
typedef enum
{
    ENTITY_SENSOR,
    ENTITY_SWITCH,
    ENTITY_BUTTON,
    ENTITY_TEXT,
    ENTITY_TAG,
    ENTITY_DEVICE_TRACKER,
    ENTITY_MAX,
} entity_type_t;

typedef struct
{
    const char *const name;                                                    // 实体名称
    const char *const unit;                                                    // 单位（仅传感器用）
    uint8_t (*get_state)(void *user_data, uint8_t id, char *buf, uint8_t len); // 获取状态回调
    void (*set_state)(void *user_data, uint8_t id, const char *state);         // 设置状态回调（仅开关用）
    void *user_data;                                                           // 用户数据
    uint16_t type;                                                             // 类型：传感器 or 开关
    uint16_t id;                                                               // 设备传感器/开关 ID
} ha_entity_s, *ha_entity_t;
#pragma pack()

// 定义一个传感器实体
#define HA_SENSOR(_name, _id, _unit, _get) \
    {ENTITY_SENSOR, _name, _id, _unit, _get, NULL}

// 定义一个开关实体
#define HA_SWITCH(_name, _id, _get, _set) \
    {ENTITY_SWITCH, _name, _id, NULL, _get, _set}

#define MQTT_ENTITY_SECTION(number) __attribute__((used, section(".mqtt_entity." #number)))
#define MQTT_ENTITY_BASE(TYPE, ID, NAME, UNIT, GET, SET, USER_DATA, LEVEL)   \
    const MQTT_ENTITY_SECTION(LEVEL) ha_entity_s __mqtt_entity_item_##NAME = \
        {                                                                    \
            .name = #NAME,                                                   \
            .unit = UNIT,                                                    \
            .get_state = GET,                                                \
            .set_state = SET,                                                \
            .user_data = USER_DATA,                                          \
            .type = TYPE,                                                    \
            .id = ID,                                                        \
    };
#define ADD_MQTT_ENTITY_TAG(ID, NAME, GET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_TAG, ID, NAME, NULL, GET, NULL, USER_DATA, tag)
#define ADD_MQTT_ENTITY_DEVICE_TRACKER(ID, NAME, GET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_DEVICE_TRACKER, ID, NAME, NULL, GET, NULL, USER_DATA, device_tracker)
#define ADD_MQTT_ENTITY_SENSOR(ID, NAME, UNIT, GET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_SENSOR, ID, NAME, UNIT, GET, NULL, USER_DATA, sensor)
#define ADD_MQTT_ENTITY_SENSOR_STR(ID, NAME, UNIT, GET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_SENSOR, ID, NAME, UNIT, GET, NULL, USER_DATA, sensor)
#define ADD_MQTT_ENTITY_TEXT(ID, NAME, GET, SET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_TEXT, ID, NAME, NULL, GET, SET, USER_DATA, text)
#define ADD_MQTT_ENTITY_SWITCH(ID, NAME, GET, SET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_SWITCH, ID, NAME, NULL, GET, SET, USER_DATA, switch)
#define ADD_MQTT_ENTITY_BUTTON(ID, NAME, SET, USER_DATA) MQTT_ENTITY_BASE(ENTITY_BUTTON, ID, NAME, NULL, NULL, SET, USER_DATA, btn)

#endif // __HA_MQTT_H__
